package org.nnxy.foodboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nnxy.foodboot.entity.Recipe;
import org.nnxy.foodboot.mapper.RecipeMapper;
import org.nnxy.foodboot.service.RecipeService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 食谱Service实现类
 */
@Service
public class RecipeServiceImpl extends ServiceImpl<RecipeMapper, Recipe> implements RecipeService {

    @Override
    public Map<String, Long> countByFlavor() {
        LambdaQueryWrapper<Recipe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(Recipe::getFlavor)
                .ne(Recipe::getFlavor, "");
        
        List<Recipe> recipes = list(queryWrapper);
        
        return recipes.stream()
                .collect(Collectors.groupingBy(Recipe::getFlavor, Collectors.counting()));
    }
    
    @Override
    public Map<String, Long> countByTechnique() {
        LambdaQueryWrapper<Recipe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(Recipe::getTechnique)
                .ne(Recipe::getTechnique, "");
        
        List<Recipe> recipes = list(queryWrapper);
        
        return recipes.stream()
                .collect(Collectors.groupingBy(Recipe::getTechnique, Collectors.counting()));
    }
    
    @Override
    public Map<String, Long> countByDifficulty() {
        LambdaQueryWrapper<Recipe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(Recipe::getDifficulty)
                .ne(Recipe::getDifficulty, "");
        
        List<Recipe> recipes = list(queryWrapper);
        
        return recipes.stream()
                .collect(Collectors.groupingBy(Recipe::getDifficulty, Collectors.counting()));
    }
    
    @Override
    public long countByTimeRange(LocalDateTime startTime, LocalDateTime endTime) {
        LambdaQueryWrapper<Recipe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.between(Recipe::getCreateTime, startTime, endTime);
        
        return count(queryWrapper);
    }
} 